package com.yx.frame.builder;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class ViewUpdateSqlTxt extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text;
	private Table sqlTable;
	private int selectIndex;	
	private Label lblSql;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public ViewUpdateSqlTxt(Shell parent, int style, Table sqlTable,
			int selectIndex) {
		super(parent, style);
		setText("Sql编辑窗口");
		this.sqlTable = sqlTable;
		this.selectIndex = selectIndex;		
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
		shell.setSize(450, 374);
		shell.setText("更新编辑窗口");

		text = new Text(shell, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		text.setBounds(10, 29, 424, 277);
		

		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String sqlTxt = text.getText();
				if ("".equals(sqlTxt)) {
					return;
				}
				if (selectIndex >= 0) {
					TableItem item = sqlTable.getItem(selectIndex);
					item.setText(1, sqlTxt);					
				} else {
					TableItem item = new TableItem(sqlTable, SWT.NONE);
					String loopTxt = "";					
					item.setText(new String[] { "" + sqlTable.getItemCount(),
							sqlTxt, loopTxt });
				}
				shell.close();
				shell.dispose();
			}
		});
		btnNewButton.setBounds(355, 312, 80, 27);
		if (selectIndex >= 0) {
			text.setText(sqlTable.getItem(selectIndex).getText(1));
			btnNewButton.setText("修改");
		}else{
			btnNewButton.setText("添加");
		}

		lblSql = new Label(shell, SWT.NONE);
		lblSql.setText("SQL语句：");
		lblSql.setBounds(10, 10, 61, 17);
	}
}
